package com.zy.baselib.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;

import com.zy.baselib.R;
import com.zy.baselib.tools.KeyboardManager;

/**
 * @author: shalei
 * @since: 2020-03-23 10:15
 * @Desc:
 **/
public class TextFiledDialog extends TipDialog {

    private AppCompatEditText mInputView;

    public TextFiledDialog(Context context) {
        super(context, R.layout.layout_dialog_textfiled, "", "", "取消", "提交", null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mInputView = findViewById(R.id.edit_input);

        findViewById(R.id.btn_left).setOnClickListener(v -> {
            if(getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                InputMethodManager manager= (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
            dismiss();
        });
        findViewById(R.id.btn_right).setOnClickListener(v -> {
            String input = mInputView.getText().toString().trim();
            if(getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                InputMethodManager manager= (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
            if(onInputListener != null) {
                onInputListener.onInput(input, TextFiledDialog.this);
            }
        });
    }

    private OnInputListener onInputListener;

    public void setOnInputListener(OnInputListener onInputListener) {
        this.onInputListener = onInputListener;
    }

    public interface OnInputListener {
        void onInput(String reason, Dialog dialog);
    }
}
